package short_semester.questionnaire_planet.service.editService;

import com.google.common.collect.Multimap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.dao.RelationDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.editRequest.QIDAndTypeBody;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.service.QuestionSequenceService;

import java.util.*;

@Service
public class QuestionService {
    
    @Autowired
    SingleChoiceQuestionService singleChoiceQuestionService;
    @Autowired
    MultiChoiceQuestionService multiChoiceQuestionService;
    @Autowired
    FillBlankQuestionService fillBlankQuestionService;
    @Autowired
    ScoreQuestionService scoreQuestionService;
    
    @Autowired
    QuestionSequenceDao sequenceDao;
    
    @Autowired
    QuestionSequenceService sequenceService;
    
    @Autowired
    RelationDao relationDao;
    
    
    //更快的接口
    //新的获取问卷信息接口
    public Collection<Object> getQuestions(Questionnaire questionnaire){
        List<Object> questionList = new LinkedList<>();
        for(QuestionSequence questionSequence: sequenceDao.findByQuestionnaireOrderBySequenceAsc(questionnaire)){
            switch (questionSequence.getType()){
                case "radio":
                    questionList.add(singleChoiceQuestionService.getQuestion(questionSequence.getQuestionId()));
                    break;
                case "checkbox":
                    questionList.add(multiChoiceQuestionService.getQuestion(questionSequence.getQuestionId()));
                    break;
                case "completion":
                    questionList.add(fillBlankQuestionService.getQuestion(questionSequence.getQuestionId()));
                    break;
                case "score":
                    questionList.add(scoreQuestionService.getQuestion(questionSequence.getQuestionId()));
                    break;
            }
        }

        return questionList;
    }

    public Collection<Object> getQuestionsRandom(Questionnaire questionnaire){
        List<Object> questionList = new ArrayList<>();
        for(QuestionSequence questionSequence: sequenceDao.findByQuestionnaireOrderBySequenceAsc(questionnaire)){
            switch (questionSequence.getType()){
                case "radio":
                    questionList.add(singleChoiceQuestionService.getQuestionRandom(questionSequence.getQuestionId()));
                    break;
                case "checkbox":
                    questionList.add(multiChoiceQuestionService.getQuestionRandom(questionSequence.getQuestionId()));
                    break;
                case "completion":
                    questionList.add(fillBlankQuestionService.getQuestion(questionSequence.getQuestionId()));
                    break;
                case "score":
                    questionList.add(scoreQuestionService.getQuestion(questionSequence.getQuestionId()));
                    break;
            }
        }
        String randomType = questionnaire.getRandomType();
        if(randomType == null||randomType.isEmpty()||relationDao.existsByQuestionnaire(questionnaire)){
            //不随机
//            Collections.shuffle(questionList);
        }else{
            //随机方式
            Collections.shuffle(questionList);
        }
        return questionList;
    }
    
    
    //新增题目
    public CommonResponse addQuestion(Questionnaire questionnaire,
                                      String type,
                                      String qtype){
        CommonResponse response = new CommonResponse();
        int sequence = getQuestionNum(questionnaire);
        int result = -1;
        try{
            switch (type){
                case "radio":
                    result = singleChoiceQuestionService.addQuestion(questionnaire,sequence,qtype);
                    break;
                case "checkbox":
                    result = multiChoiceQuestionService.addQuestion(questionnaire,sequence,qtype);
                    break;
                case "completion":
                    result = fillBlankQuestionService.addQuestion(questionnaire,sequence,qtype);
                    break;
                case "score":
                    result = scoreQuestionService.addQuestion(questionnaire,sequence,qtype);
                    break;
            }
            
            if(result==-1){
                response.error("新增问题错误");
                return response;
            }
            
            Map<String, Integer> resultMap = new HashMap<>();
            resultMap.put("questionId",result);
            response.setData(resultMap);
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    //删除题目
    public CommonResponse deleteQuestion(Integer questionId,String type){
        CommonResponse response = new CommonResponse();
        try {
            switch (type){
                case "radio":
                    singleChoiceQuestionService.deleteQuestion(questionId);
                    break;
                case "checkbox":
                    multiChoiceQuestionService.deleteQuestion(questionId);
                    break;
                case "completion":
                    fillBlankQuestionService.deleteQuestion(questionId);
                    break;
                case "score":
                    scoreQuestionService.deleteQuestion(questionId);
                    break;
            }
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    //获得问卷题目数量
    public int getQuestionNum(Questionnaire questionnaire){
        
        int sequence = sequenceDao.countByQuestionnaire(questionnaire);
        
//        return singleChoiceQuestionService.getQuestionNum(questionnaire)+
//                multiChoiceQuestionService.getQuestionNum(questionnaire)+
//                scoreQuestionService.getQuestionNum(questionnaire)+
//                fillBlankQuestionService.getQuestionNum(questionnaire);
        return sequence;
    }
    
    //改变顺序
    public CommonResponse changeOrder(List<QIDAndTypeBody> questionIds){
        CommonResponse response = new CommonResponse();
        
        try{
            
            int sequence = 0;
            for(QIDAndTypeBody body: questionIds){
                switch (body.getType()){
                    case "radio":
                        singleChoiceQuestionService.setSequenceById(body.getQuestionId(),sequence);
                        break;
                    case "checkbox":
                        multiChoiceQuestionService.setSequenceById(body.getQuestionId(),sequence);
                        break;
                    case "completion":
                        fillBlankQuestionService.setSequenceById(body.getQuestionId(),sequence);
                        break;
                    case "score":
                        scoreQuestionService.setSequenceById(body.getQuestionId(),sequence);
                        break;
                }
                
                ++sequence;
            }
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    //拷贝问卷
    public void copyQuestions(Questionnaire questionnaire,Questionnaire oldQuestionnaire){
        singleChoiceQuestionService.copyQuestion(questionnaire,oldQuestionnaire);
        multiChoiceQuestionService.copyQuestion(questionnaire,oldQuestionnaire);
        fillBlankQuestionService.copyQuestion(questionnaire,oldQuestionnaire);
        scoreQuestionService.copyQuestion(questionnaire,oldQuestionnaire);
    }
}
